#include <QtGui>
#include "meascfgdlg.h"
#include "../core/aprcfg.h"
#include "../core/measprog.h"

MeasCfgDlg::MeasCfgDlg(QWidget *parent)
    :QDialog(parent)
{
    // Create cfg controls
    QLabel* lblMeasMode = new QLabel(tr("AUTO/MANUAL"));
    lblMeasMode->setAlignment(Qt::AlignCenter);
    cmbMeasMode = new QComboBox;
    QStringList strListMeasMode;
    //strListMeasMode << tr("AUTO") << tr("REPEAT") << tr("MANUAL");
	AprCfg::getNamesByAttr(PM_MMODE_ATTR, strListMeasMode);
    cmbMeasMode->addItems(strListMeasMode);
    ckbFfs    = new QCheckBox(tr("FFS ON/OFF"));
    ckbCntClr = new QCheckBox(tr("COUNTER CLEAR"));
    ckbDList  = new QCheckBox(tr("DLIST ON/OFF"));
    ckbTList  = new QCheckBox(tr("TLIST ON/OFF"));
    ckbNonDis = new QCheckBox(tr("NON-DISPLAY"));

    QGridLayout* glayout = new QGridLayout;
    glayout->addWidget(lblMeasMode, 0, 0, 1, 1);
    glayout->addWidget(cmbMeasMode, 0, 1, 1, 1);
    glayout->addWidget(ckbCntClr, 0, 2, 1, 2);
    glayout->addWidget(ckbDList, 1, 0, 1, 1);
    glayout->addWidget(ckbTList, 1, 1, 1, 1);
    glayout->addWidget(ckbFfs, 1, 2, 1, 1);
    glayout->addWidget(ckbNonDis, 1, 3, 1, 1);

	buttonBox = new QDialogButtonBox;
	QPushButton* btnOk = buttonBox->addButton(tr("OK"), QDialogButtonBox::ActionRole);
    buttonBox->addButton(tr("CANCEL"), QDialogButtonBox::RejectRole);

    QVBoxLayout* vlayout = new QVBoxLayout;
    vlayout->addLayout(glayout);
	vlayout->addWidget(buttonBox);

    setLayout(vlayout);
    setWindowTitle(tr("MEAS SETTINGS"));

	// load meas config from MeasProg and init controls
	loadMeasCfg();

	QObject::connect(btnOk, SIGNAL(clicked()), this, SLOT(saveMeasCfg()));
	QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

void MeasCfgDlg::saveMeasCfg()
{
	short ffs		= ckbFfs->isChecked() ? PM_ON : PM_OFF;	
	short cntClr 	= ckbCntClr->isChecked() ? PM_ON : PM_OFF;	
	short dList		= ckbDList->isChecked() ? PM_ON : PM_OFF;	
	short tList		= ckbTList->isChecked() ? PM_ON : PM_OFF;	
	short nonDis	= ckbNonDis->isChecked() ? PM_ON : PM_OFF;
//	short mMode		= cmbMeasMode->currentIndex() + PM_AUTO;
	QString strMode = cmbMeasMode->currentText();
	ushort mMode = 0;
	AprCfg::getParamCodeByName(strMode, PM_MMODE_ATTR, mMode);

	MeasProg* pMeasProg = MeasProg::getInstance();
	program_s* p = pMeasProg->getProgPtr();
	
	p->ffs 		= ffs;
	p->cntmode	= cntClr;
	p->dliston	= dList;
	p->tliston	= tList;
	p->nondisp	= nonDis;
	p->mmode	= mMode;

	accept();
}

void MeasCfgDlg::loadMeasCfg()
{
	MeasProg* pMeasProg = MeasProg::getInstance();
	program_s* pProgram = pMeasProg->getProgPtr();

	bool ffs	= (PM_ON == pProgram->ffs);
	bool cntClr	= (PM_ON == pProgram->cntmode);
	bool dList	= (PM_ON == pProgram->dliston);
	bool tList	= (PM_ON == pProgram->tliston);
	bool nonDis	= (PM_ON == pProgram->nondisp);
	QString strMode;
	AprCfg::getParamNameByCode(pProgram->mmode, strMode);
	int mModeIndex = cmbMeasMode->findText(strMode);

	ckbFfs->setChecked(ffs);
	ckbCntClr->setChecked(cntClr);
	ckbDList->setChecked(dList);
	ckbTList->setChecked(tList);
	ckbNonDis->setChecked(nonDis);
	cmbMeasMode->setCurrentIndex(mModeIndex);
}
